const {
  DataTypes
} = require('sequelize');
module.exports = sequelize => {
  const attributes = {
    id: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "主键",
      primaryKey: true,
      field: "id",
      autoIncrement: true
    },
    name: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "部门名称",
      primaryKey: false,
      field: "name",
      autoIncrement: false
    },
    code: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "部门编号",
      primaryKey: false,
      field: "code",
      autoIncrement: false,
      unique: "uk_code"
    },
    parentId: {
      type: DataTypes.BIGINT,
      allowNull: false,
      defaultValue: null,
      comment: "父节点id",
      primaryKey: false,
      field: "parent_id",
      autoIncrement: false
    },
    treePath: {
      type: DataTypes.CHAR,
      allowNull: false,
      defaultValue: null,
      comment: "父节点id路径",
      primaryKey: false,
      field: "tree_path",
      autoIncrement: false
    },
    sort: {
      type: DataTypes.INTEGER,
      allowNull: true,
      defaultValue: null,
      comment: "显示顺序",
      primaryKey: false,
      field: "sort",
      autoIncrement: false
    },
    status: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: "状态(1-正常 0-禁用)",
      primaryKey: false,
      field: "status",
      autoIncrement: false
    },
    createBy: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "创建人ID",
      primaryKey: false,
      field: "create_by",
      autoIncrement: false
    },
    createTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "创建时间",
      primaryKey: false,
      field: "create_time",
      autoIncrement: false
    },
    updateBy: {
      type: DataTypes.BIGINT,
      allowNull: true,
      defaultValue: null,
      comment: "修改人ID",
      primaryKey: false,
      field: "update_by",
      autoIncrement: false
    },
    updateTime: {
      type: DataTypes.DATE,
      allowNull: true,
      defaultValue: null,
      comment: "更新时间",
      primaryKey: false,
      field: "update_time",
      autoIncrement: false
    },
    isDeleted: {
      type: DataTypes.INTEGER,
      allowNull: false,
      defaultValue: null,
      comment: "逻辑删除标识(1-已删除 0-未删除)",
      primaryKey: false,
      field: "is_deleted",
      autoIncrement: false
    }
  };
  const options = {
    tableName: "sys_dept",
    comment: "",
    indexes: []
  };
  const SysDeptModel = sequelize.define("sys_dept_model", attributes, options);

  // 添加关联关系
  SysDeptModel.associate = function(models) {
    // 与用户的关联关系
    SysDeptModel.hasMany(models.sys_user_model, {
      foreignKey: 'dept_id',
      as: 'users'
    });
  };

  return SysDeptModel;
};